package com.example.qfsx;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.qfsx.bean.CartItem;
import com.example.qfsx.bean.CartItem2;
import com.example.qfsx.bean.ItemAdapter;
import com.example.qfsx.bean.NetworkUtils;

import java.util.ArrayList;
import java.util.List;

public class CartActivity extends AppCompatActivity {
    private ItemAdapter itemAdapter;
    private List<CartItem> itemList;
    Context _this = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_cart);
        _this = this;
        ListView listView = findViewById(R.id.list_view);
        try {
            new NetworkUtils().fetchItems(new NetworkUtils.ItemFetchCallback() {
                @Override
                public void onSuccess(List<CartItem2> items) {
                    runOnUiThread(() -> {
                        ItemAdapter adapter = new ItemAdapter(_this, items);
                        listView.setAdapter(adapter);
                    });
                }

                @Override
                public void onFailure(String error) {
                    runOnUiThread(() -> Toast.makeText(_this, "Error: " + error, Toast.LENGTH_SHORT).show());
                }
            },_this);
        }catch (Exception e){
            System.out.println("qf123"+e.toString());
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}